"""

Caution:
  - if lxml (python-lxml) is not available, libxml-dev and libxslt1-dev 
    (and GCC) must be available so that lxml can be built by easy_install.
"""

from datetime import datetime
import logging
import re

try:
    from setuptools import setup, find_packages
except ImportError:
    from ez_setup import use_setuptools
    use_setuptools()
    from setuptools import setup, find_packages

log = logging.getLogger(__file__)
logging.basicConfig(level=logging.DEBUG)

python_version = "2.5"
site_package = "/usr/lib/python%s/site-packages/" % python_version
egg_re = "%s(?P<name>.*?)-(?P<version>.*?)-py%s.egg/.*" % \
          (re.escape(site_package), re.escape(python_version))
reg = re.compile(egg_re, re.IGNORECASE)


def get_same_version(package_name, version_operator="=="):
  """Returns the string needed to put in install_requires list to have
  the package at the exact version it is installed in this environment.
  
  Arguments:
    - package_name: string, the name of the package to consider
    - version_operator: string, one of: "==", ">=", "!="
  """
  try:
    module = __import__(package_name, level=0)
    res = reg.match(module.__file__)
    if res:
      name = res.groupdict()['name']
      # If different, treat the case by hand...
      assert name.lower() == package_name, name
      version = res.groupdict()['version']
      return "%s%s%s" % (name, version_operator, version)
    log.debug("RE does not match for %s (%s)." % (package_name, module.__file__))
    return "%s%s%s" % (package_name, version_operator, module.__version__)
  except:
    log.error("Impossible to determine the version of %s" % package_name)
    raise


def get_versions():
  """
  """
  versions = ["sqlalchemy", "nose", "pylons"]
  versions = [get_same_version(v) for v in versions]
  versions.extend(["lxml>=2.1.1-1",
                   "tw.forms>=0.9.2",
                   ])
  log.debug(versions)
  return versions


setup(
    name='ciby-net',
    #version="0.1",
    version="0.1."+".".join([str(e) for e in datetime.now().utctimetuple()[:-3]]),
    description='Network access administration interface.',
    author='Pierre Ruyssen',
    author_email='Pierre.Ruyssen__A_T__gmail.com',
    #url='',
    install_requires=get_versions(),
    packages=find_packages(exclude=['ez_setup']),
    include_package_data=True,
    test_suite='nose.collector',
    package_data={'cibynet': ['i18n/*/LC_MESSAGES/*.mo']},
    #message_extractors = {'cibynet': [
    #        ('**.py', 'python', None),
    #        ('templates/**.mako', 'mako', None),
    #        ('public/**', 'ignore', None)]},
    entry_points="""
    [paste.app_factory]
    main = cibynet.config.middleware:make_app

    [paste.app_install]
    main = pylons.util:PylonsInstaller
    """,
)
